/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.EstPriceDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import com.minierp.helper.PageHelper;
import com.minierp.model.EstPrice;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class EstPriceDaoImpl extends BaseDaoImpl implements EstPriceDao {
	public List getList(String estType, long mtId, String priceType,
			String proName, long estId, int startIndex, int num)
			throws Exception {
		String str = "";
		if (mtId != 0L) {
			str = str + " and (mt.id=" + mtId
					+ " or (mt.parent_id_str like \'%%;" + mtId + ";\'))";
		}

		if (priceType != null && !priceType.equals("")) {
			str = str + " and p.price_type = :priceType";
		}

		if (proName != null) {
			proName = proName.trim();
		}

		if (proName != null && !proName.equals("")) {
			str = str
					+ " and ((p.pro_name like :proName) or (p.pro_spell like :proName))";
		}

		if (estId != 0L) {
			str = str + " and p.est_id=" + estId;
		}

		String sql = "select p.id,mt.type_name,p.pro_name,p.unit,p.price,p.make_require,p.mark,p.length,p.width,p.specification,p.attach_file from est_price p left join make_type mt on p.make_type_id=mt.id where p.est_type=:estType"
				+ str + " order by mt.tn_spell,p.pro_name,p.specification";
		Session sess = null;

		List arg14;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			q.setString("estType", estType);
			if (priceType != null && !priceType.equals("")) {
				q.setString("priceType", priceType);
			}

			if (proName != null && !proName.equals("")) {
				q.setString("proName", "%" + proName + "%");
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg14 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg14;
	}

	public int getListCount(String estType, long mtId, String priceType,
			String proName, long estId) throws Exception {
		String str = "";
		if (mtId != 0L) {
			str = str + " and (mt.id=" + mtId
					+ " or (mt.parent_id_str like \'%%;" + mtId + ";\'))";
		}

		if (priceType != null && !priceType.equals("")) {
			str = str + " and p.price_type = :priceType";
		}

		if (proName != null) {
			proName = proName.trim();
		}

		if (proName != null && !proName.equals("")) {
			str = str
					+ " and ((p.pro_name like :proName) or (p.pro_spell like :proName))";
		}

		if (estId != 0L) {
			str = str + " and p.est_id=" + estId;
		}

		String sql = "select count(p.id)  from est_price p left join make_type mt on p.make_type_id=mt.id where p.est_type=:estType"
				+ str;
		Session sess = null;

		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			q.setString("estType", estType);
			if (priceType != null && !priceType.equals("")) {
				q.setString("priceType", priceType);
			}

			if (proName != null && !proName.equals("")) {
				q.setString("proName", "%" + proName + "%");
			}

			List list = q.list();
			if (!list.isEmpty()) {
				int arg13 = Integer.parseInt(list.get(0).toString());
				return arg13;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}

	public List getListForSelect(String estType, long mtId, String priceType,
			String proName, long estId, String unitRang, int startIndex, int num)
			throws Exception {
		String str = "";
		if (mtId != 0L) {
			str = str + " and (mt.id=" + mtId
					+ " or (mt.parent_id_str like \'%%;" + mtId + ";\'))";
		}

		if (priceType != null && !priceType.equals("")) {
			str = str + " and p.price_type = :priceType";
		}

		if (proName != null) {
			proName = proName.trim();
		}

		if (proName != null && !proName.equals("")) {
			str = str
					+ " and ((p.pro_name like :proName) or (p.pro_spell like :proName))";
		}

		if (estId != 0L) {
			str = str + " and p.est_id=" + estId;
		}

		if (unitRang != null) {
			if (unitRang.equals("AREA")) {
				str = str
						+ " and (p.unit=\'平方米\' or p.unit=\'平米\' or p.unit=\'㎡\')";
			} else if (unitRang.equals("LENG")) {
				str = str
						+ " and (p.unit=\'米\' or p.unit=\'公分\' or p.unit=\'厘米\' or p.unit=\'m\' or p.unit=\'cm\')";
			}
		}

		String sql = "select p.make_type_id,mt.type_name,p.pro_name,p.unit,p.price,p.make_require,p.mark,mt.work_center_id,wc.wc_name,p.length,p.width,p.specification,p.attach_file,mt.type,p.pro_id from est_price p left join make_type mt on p.make_type_id=mt.id left join work_center wc on wc.id=mt.work_center_id where p.est_type=:estType"
				+ str + " order by p.id";
		Session sess = null;

		List arg15;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			q.setString("estType", estType);
			if (priceType != null && !priceType.equals("")) {
				q.setString("priceType", priceType);
			}

			if (proName != null && !proName.equals("")) {
				q.setString("proName", "%" + proName + "%");
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg15 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg15;
	}

	public int getListForSelectCount(String estType, long mtId,
			String priceType, String proName, long estId, String unitRang)
			throws Exception {
		String str = "";
		if (mtId != 0L) {
			str = str + " and (mt.id=" + mtId
					+ " or (mt.parent_id_str like \'%%;" + mtId + ";\'))";
		}

		if (priceType != null && !priceType.equals("")) {
			str = str + " and p.price_type = :priceType";
		}

		if (proName != null) {
			proName = proName.trim();
		}

		if (proName != null && !proName.equals("")) {
			str = str
					+ " and ((p.pro_name like :proName) or (p.pro_spell like :proName))";
		}

		if (estId != 0L) {
			str = str + " and p.est_id=" + estId;
		}

		if (unitRang != null) {
			if (unitRang.equals("AREA")) {
				str = str
						+ " and (p.unit=\'平方米\' or p.unit=\'平米\' or p.unit=\'㎡\')";
			} else if (unitRang.equals("LENG")) {
				str = str
						+ " and (p.unit=\'米\' or p.unit=\'公分\' or p.unit=\'厘米\' or p.unit=\'m\' or p.unit=\'cm\')";
			}
		}

		String sql = "select count(p.id) from est_price p left join make_type mt on p.make_type_id=mt.id left join work_center wc on wc.id=mt.work_center_id where p.est_type=:estType"
				+ str;
		Session sess = null;

		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			q.setString("estType", estType);
			if (priceType != null && !priceType.equals("")) {
				q.setString("priceType", priceType);
			}

			if (proName != null && !proName.equals("")) {
				q.setString("proName", "%" + proName + "%");
			}

			List list = q.list();
			if (!list.isEmpty()) {
				int arg14 = Integer.parseInt(list.get(0).toString());
				return arg14;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}

	public List getList(String estType, String priceType, long estId) {
		String sql = "select e.make_type_id,e.pro_name,e.make_require,e.unit,e.price,e.mark,e.length,e.width,e.specification,e.pro_id from est_price e where e.price_type=:priceType and e.est_type=:estType and e.est_id="
				+ estId + " order by e.id desc";
		Session sess = null;

		List arg8;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			q.setString("priceType", priceType);
			q.setString("estType", estType);
			arg8 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg8;
	}

	public List getList(String estType, String priceType, String[] mtIdArr) {
		if (mtIdArr != null && mtIdArr.length != 0) {
			String str = "select id from make_type where "
					+ PageHelper.createIdArrStr(mtIdArr, "id", false);

			for (int sql = 0; sql < mtIdArr.length; ++sql) {
				str = str
						+ " union all select id from make_type where parent_id_str like \'%;"
						+ mtIdArr[sql] + ";%\'";
			}

			String arg11 = "select e.make_type_id,e.pro_name,e.make_require,e.unit,e.price,e.mark,e.length,e.width,e.specification,e.pro_id from est_price e,("
					+ str
					+ ") a"
					+ " where e.make_type_id=a.id"
					+ " and e.price_type=:priceType"
					+ " and e.est_type=:estType" + " order by e.id desc";
			Session sess = null;

			List arg8;
			try {
				sess = this.getSession();
				SQLQuery q = sess.createSQLQuery(arg11);
				q.setString("priceType", priceType);
				q.setString("estType", estType);
				arg8 = q.list();
			} finally {
				if (sess != null) {
					this.releaseSession(sess);
				}

			}

			return arg8;
		} else {
			return new ArrayList();
		}
	}

	public int delete(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.EstPrice where id=:id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int estCorporate(final long fromClientId, final long toClientId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.EstPrice set estId="
								+ toClientId + " where estId=" + fromClientId;
						Query q = session.createQuery(hql);
						return new Integer(q.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public EstPrice get(String estType, String priceType, long estId,
			long mtId, long proId, String proName) {
		String sql = "from com.minierp.model.EstPrice e where e.priceType=:priceType and e.estType=:estType and e.estId="
				+ estId + " and e.makeTypeId=" + mtId;
		if (proId != 0L) {
			sql = sql + " and e.proId=" + proId;
		} else {
			sql = sql + " and e.proName=:proName";
		}

		sql = sql + " order by e.id desc";
		Session sess = null;

		EstPrice arg14;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setString("priceType", priceType);
			q.setString("estType", estType);
			if (proId == 0L) {
				q.setString("proName", proName);
			}

			List list = q.list();
			if (list.isEmpty()) {
				return null;
			}

			arg14 = (EstPrice) list.get(0);
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg14;
	}

	public List getList(long estId) {
		String sql = "select e.make_type_id,e.pro_name,e.price from est_price e where e.est_id="
				+ estId;
		Session sess = null;

		List arg6;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			arg6 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg6;
	}
}